package com.zg.http;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 响应结果
 */
public final class ResponseBean {

	private ResponseBean() {
	}

	/**
	 * ThreadLocal级的响应消息
	 */
	private static final ThreadLocal<Map<String, Object>> threadLocalEntity = new ThreadLocal<Map<String, Object>>() {
		@Override
		protected Map<String, Object> initialValue() {
			return new HashMap<String, Object>();
		}
	};

	/**
	 * 获取响应消息
	 * @return
	 */
	public static String _getMessage() {
		return (String)threadLocalEntity.get().get("message");
	}

	/**
	 * 获取业务状态
	 * @return
	 */
	public static String _getStatus() {
		return (String)threadLocalEntity.get().get("status");
	}

	/**
	 * 获取HTTP状态码
	 * @return
	 */
	public static Integer _getCode() {
		return (Integer)threadLocalEntity.get().get("code");
	}

	/**
	 * 获取响应实体
	 * @return
	 */
	public static Map<String, Object> getEntity() {
		return threadLocalEntity.get();
	}

	/**
	 * 设置响应实体
	 * @param code
	 * @param status
	 * @param data
	 */
	public static void set(Integer code, String status, Object data) {
		Map<String, Object> map = threadLocalEntity.get();
		map.put("code", code);
		map.put("status", StringUtils.isBlank(status) ? "yes" : status);
		map.put("data", data);
	}

	/**
	 * 设置业务状态为no
	 */
	public static void no() {
		Map<String, Object> map = threadLocalEntity.get();
		map.put("status", "no");
	}

	/**
	 * 设置业务状态为no，设置message
	 * @param message
	 */
	public static void no(String message) {
		if (StringUtils.isBlank(message)) {
			throw new IllegalArgumentException("message不能为空");
		}

		Map<String, Object> map = threadLocalEntity.get();
		map.put("status", "no");
		map.put("message", message);
	}

	/**
	 * 设置业务状态为no，设置code
	 * @param code
	 */
	public static void no(Integer code) {
		if (null == code) {
			throw new IllegalArgumentException("code不能为空");
		}

		Map<String, Object> map = threadLocalEntity.get();
		map.put("status", "no");
		map.put("code", code);
	}

	/**
	 * 设置业务状态为no，设置http响应码，设置message
	 * @param code
	 * @param status
	 * @param message
	 */
	public static void no(Integer code, String message) {
		if (null == code) {
			throw new IllegalArgumentException("code不能为空");
		}

		Map<String, Object> map = threadLocalEntity.get();
		map.put("status", "no");
		map.put("code", code);
		map.put("message", message);
	}

	/**
	 * 设置业务状态为yes
	 */
	public static void yes() {
		Map<String, Object> map = threadLocalEntity.get();
		map.put("status", "yes");
	}

	/**
	 * 设置业务状态为yes，设置message
	 * @param message
	 */
	public static void yes(String message) {
		if (StringUtils.isBlank(message)) {
			throw new IllegalArgumentException("message不能为空");
		}

		Map<String, Object> map = threadLocalEntity.get();
		map.put("status", "yes");
		map.put("message", message);
	}

	/**
	 * 设置业务状态为yes，设置code
	 * @param code
	 */
	public static void yes(Integer code) {
		if (null == code) {
			throw new IllegalArgumentException("code不能为空");
		}

		Map<String, Object> map = threadLocalEntity.get();
		map.put("status", "yes");
		map.put("code", code);
	}

	/**
	 * 设置业务状态为yes，设置http响应码，设置message
	 * @param code
	 * @param status
	 * @param message
	 */
	public static void yes(Integer code, String message) {
		if (null == code || StringUtils.isBlank(message)) {
			throw new IllegalArgumentException("code,message不能为空");
		}

		Map<String, Object> map = threadLocalEntity.get();
		map.put("status", "yes");
		map.put("code", code);
		map.put("message", message);
	}

	/**
	 * 获取响应内容
	 * @param key
	 * @return
	 */
	public static Object get(String key) {
		return threadLocalEntity.get().get(key);
	}

	/**
	 * 设置响应内容
	 * @param key
	 * @param obj
	 */
	public static void set(String key, Object obj) {
		threadLocalEntity.get().put(key, obj);
	}

    /**
	 * 清除响应实体
	 */
	public static void clear() {
		threadLocalEntity.get().clear();
	}
}